import os
import sys
import logging
import logging.handlers

from mycelery import config

def init_logger():
    # logging.getLogger('tornado.access').disabled = True

    config.logger = logging.getLogger()
    config.logger.propagate = False
    log_dir = os.path.join(config.BASE_DIRS, 'log')

    if not os.path.exists(log_dir):
        os.mkdir(log_dir)
    log_file = os.path.join(log_dir, config.log_file)
    hdlr = logging.handlers.TimedRotatingFileHandler(log_file, when='D', interval=1, backupCount=40)
    formatter = logging.Formatter(config.LOGGING_FORMAT)
    hdlr.setFormatter(formatter)
    config.logger.addHandler(hdlr)

    stream_hdlr = logging.StreamHandler(sys.stdout)
    stream_hdlr.setFormatter(formatter)
    config.logger.addHandler(stream_hdlr)
    config.logger.setLevel(config.LOGGING_LEVEL)
